package com.demo.example.proxy.cglib;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

public class CheckInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        check(objects);
        return methodProxy.invokeSuper(o, objects);
    }
    
    private void check(Object[] objects) {
        boolean b = Arrays.stream(objects).allMatch(Objects::nonNull);
        System.out.printf("[%s] 正在进行参数检查%n", new Date());
        if (b) {
            System.out.printf("[%s] 参数检查通过%n", new Date());
        } else {
            throw new RuntimeException("存在非法参数");
        }
    }
}
